package com.yeshine.opendata.service.impl;

import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.IAppDao;
import com.yeshine.opendata.entity.App;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.dto.StatisticsResult;
import com.yeshine.opendata.service.IAppService;
import com.yeshine.opendata.utils.FileUtils;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import com.yeshine.opendata.utils.UploadConstantPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created by taohj on 2014/8/1.
 */
@Service
public class AppServiceImpl extends ServiceBase<App> implements IAppService {


    private IAppDao dao;

    @Autowired
    public void setDao(IAppDao dao) {
        this.dao = dao;
    }

    private String path = UploadConstantPath.APP_FILE_PATH;

    @Override
    public Result<App> create(App app) {
        app.setStatus(new SysDictionary(111));
        return new Result<App>(dao.insert(app) > 0);
    }

    @Override
    public Result<App> remove(App app) {
        return new Result<App>(dao.delete(app) > 0);
    }

    @Override
    public Result<App> modify(App app) {
        return new Result<App>(dao.update(app) > 0);
    }

    @Override
    public Result<App> getById(Integer id) {
        return new Result<App>(dao.selectById(id));
    }

    @Override
    public Result<App> getList(App app) {
        return new Result<App>(dao.selectList(app));
    }

    @Override
    public Result<App> getByPage(PageResult<App> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<App>(pageResult);
    }

    @Override
    public Result<App> create(App app, HttpServletRequest request) {


        Result<App> result = this.create(app,request);
        if (!result.isSuccess()) {
            return result;
        }

        MultipartFile image = app.getImageFile();

        if (image != null && image.getSize() > 0) {
            app.setImage(this.path + app.getId() + "_" + image.getOriginalFilename());
            try {
                FileUtils.copy(image.getInputStream(), new File(request.getSession().getServletContext().getRealPath("/"), app.getImage()));
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<App>(false, e.getMessage());
            }
        } else {
            app.setImage("resources/images/app/default.jpg");
        }

        if (app.getStorage().getId() != null && app.getStorage().getId() == 1) {
            MultipartFile file = app.getFile();
            if (file != null && file.getSize() > 0) {
                app.setUrl(this.path + app.getId() + "_" + file.getOriginalFilename());
                try {
                    FileUtils.copy(file.getInputStream(), new File(request.getSession().getServletContext().getRealPath("/"), app.getUrl()));
                } catch (IOException e) {
                    e.printStackTrace();
                    return new Result<App>(false, e.getMessage());
                }
            }
        }
        return this.modify(app);
    }

    @Override
    public Result<App> modify(App app, HttpServletRequest request) {

        String path = this.path;

        App oldApp = this.getById(app.getId()).getTarget();
        if (oldApp == null) {
            return new Result<App>(false, "该应用不存在");
        }


        MultipartFile image = app.getImageFile();
        if (image != null && image.getSize() > 0) {
            app.setImage(path + app.getId() + "_" + image.getOriginalFilename());
            try {
                FileUtils.copy(image.getInputStream(), new File(request.getSession().getServletContext().getRealPath("/"), app.getImage()));
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<App>(false, e.getMessage());
            }
        } else {
            app.setImage(oldApp.getImage());
        }


        if (app.getStorage() == null || app.getStorage().getId() == null) {
            app.setUrl(oldApp.getUrl());
        } else if (app.getStorage().getId() == 131) {
            MultipartFile file = app.getFile();
            if (file != null && file.getSize() > 0) {
                app.setUrl(path + app.getId() + "_" + file.getOriginalFilename());
                try {
                    FileUtils.copy(file.getInputStream(), new File(request.getSession().getServletContext().getRealPath("/"), app.getUrl()));
                } catch (IOException e) {
                    e.printStackTrace();
                    return new Result<App>(false, e.getMessage());
                }
            } else {
                app.setUrl(oldApp.getUrl());
            }
        } else {
            if (!StringUtils.hasLength(app.getUrl())) {
                app.setUrl(oldApp.getUrl());
            }
        }

        return this.modify(app);
    }

    @Override
    public Result<StatisticsResult> getGroupByTheme(App app) {
        return new Result<StatisticsResult>(dao.selectGroupByTheme(app));
    }

    @Override
    public Result<StatisticsResult> getGroupByPlatForm(App app) {
        return new Result<StatisticsResult>(dao.selectGroupByPlatForm(app));
    }

    @Override
    public Result<App> audit(Integer auditStatus, Integer[] apps) {
        return new Result<App>(dao.audit(auditStatus, apps)>0);
    }
}
